﻿using AdinZ.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;


/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/3/31 16:46:03
*
* */
namespace Novel.Sys.Dal
{
    /* 小说分类 */
    public class novel_sort
    {
        /// <summary>
        /// 根据关键字获取实体
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public Model.novel_sort GetModel(string names)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [novel_sort] ");
            strSql.Append(" where names=@names");
            SqlParameter[] parameters = {
                    new SqlParameter("@names", SqlDbType.NVarChar,20)};
            parameters[0].Value = names;

            Model.novel_sort model = new Model.novel_sort();
            DataSet ds = DbHelpers.GetDataSet(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["names"] != null && ds.Tables[0].Rows[0]["names"].ToString() != "")
                {
                    model.Names = ds.Tables[0].Rows[0]["names"].ToString();
                }
                if (ds.Tables[0].Rows[0]["description"] != null && ds.Tables[0].Rows[0]["description"].ToString() != "")
                {
                    model.Description = ds.Tables[0].Rows[0]["description"].ToString();
                }

                if (ds.Tables[0].Rows[0]["Keyword"] != null && ds.Tables[0].Rows[0]["Keyword"].ToString() != "")
                {
                    model.Keyword = ds.Tables[0].Rows[0]["Keyword"].ToString();
                }
                return model;
            }
            else{return null;}
        }

        public Model.novel_sort GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [novel_sort] ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int)};
            parameters[0].Value = ID;

            Model.novel_sort model = new Model.novel_sort();
            DataSet ds = DbHelpers.GetDataSet(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["names"] != null && ds.Tables[0].Rows[0]["names"].ToString() != "")
                {
                    model.Names = ds.Tables[0].Rows[0]["names"].ToString();
                }
                if (ds.Tables[0].Rows[0]["description"] != null && ds.Tables[0].Rows[0]["description"].ToString() != "")
                {
                    model.Description = ds.Tables[0].Rows[0]["description"].ToString();
                }

                if (ds.Tables[0].Rows[0]["Keyword"] != null && ds.Tables[0].Rows[0]["Keyword"].ToString() != "")
                {
                    model.Keyword = ds.Tables[0].Rows[0]["Keyword"].ToString();
                }
                return model;
            }
            else { return null; }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM novel_sort ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelpers.GetDataSet(strSql.ToString());
        }
        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM novel_sort ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelpers.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strWhere = " Where " + strWhere;
            }

            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                orderby = " Order By " + orderby;
            }

            //startIndex = startIndex - 1;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM novel_sort Where ID Not IN ");
            strSql.Append("(Select Top " + startIndex + " ID From novel_sort" + strWhere + orderby + ")");
            strSql.Append(" And ID In ");
            strSql.Append("(Select Top " + endIndex + " ID From novel_sort" + strWhere + orderby + ")");
            strSql.Append(orderby);
            return DbHelpers.GetDataSet(strSql.ToString());
        }


        public string GetSortName(int Tid)
        {
            string result = "";
            string sql = "SELECT Name,[names] FROM novel_sort WHERE ID =" + Tid;
            DataSet ds = DbHelpers.GetDataSet(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {

                //if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                //{
                //    result = ds.Tables[0].Rows[0]["Name"].ToString();
                //}
                if (ds.Tables[0].Rows[0]["names"] != null && ds.Tables[0].Rows[0]["names"].ToString() != "")
                {
                    result = ds.Tables[0].Rows[0]["names"].ToString();
                }

            }
            return result;
        }


    }

}

